<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="queryParams.role" placeholder="请选择角色">
          <el-option
            v-for="dict in dict.type.agent_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="aUserList" @selection-change="handleSelectionChange">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="头像" align="center" prop="headImg" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.headImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="是否审核" align="center" key="isExamine">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isExamine"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="角色" align="center" prop="role" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agent_level" :value="scope.row.role"/>
        </template>
      </el-table-column>
      <el-table-column label="下载数量" align="center" prop="downloadNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleIdentity(scope.row)"
          >调整身份</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改小程序用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="逻辑删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="是否上线" prop="isShow">
          <el-input v-model="form.isShow" placeholder="请输入是否上线" />
        </el-form-item>
        <el-form-item label="排序" prop="numSoft">
          <el-input v-model="form.numSoft" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="小程序openid" prop="miniOpenId">
          <el-input v-model="form.miniOpenId" placeholder="请输入小程序openid" />
        </el-form-item>
        <el-form-item label="appOpenId" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入appOpenId" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="headImg">
          <el-input v-model="form.headImg" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="form.role" placeholder="请输入角色" />
        </el-form-item>
        <el-form-item label="下载数量" prop="downloadNum">
          <el-input v-model="form.downloadNum" placeholder="请输入下载数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
<!--        <el-button @click="cancel">取 消</el-button>-->
      </div>
    </el-dialog>

    <!-- 添加或修改小程序用户对话框 -->
    <el-dialog :title="title" :visible.sync="openIdentity" width="800px" append-to-body>
      <el-descriptions class="margin-top" title="当前用户信息" :column="3" size="mini" border style="padding-bottom: 50px;">
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            昵称
          </template>
          {{form.nickName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            手机号
          </template>
          {{form.phone}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            下载数
          </template>
          {{form.downloadNum}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            角色
          </template>
          <dict-tag :options="dict.type.agent_level" :value="form.role"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            创建时间
          </template>
          {{form.createTime}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-custom"></i>
            头像
          </template>
          <image-preview :src="form.headImg" style="width: 50px; height: 50px;" />
        </el-descriptions-item>
      </el-descriptions>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="角色" prop="newRole">
          <el-select v-model="form.newRole" placeholder="请选择角色" @change="handleRoleChange">
            <el-option
              v-for="dict in dict.type.agent_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              :disabled="form.role == dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 代理商 -->
        <template v-if="form.role == '0'">
          <!-- 团队成员 -->
          <template v-if="form.newRole == '1'">
            <el-form-item label="团队" prop="teamId">
              <el-select v-model="form.teamId" placeholder="请选择团队">
                <el-option
                  v-for="aTeam in aTeamList"
                  :key="aTeam.id"
                  :label="aTeam.teName"
                  :value="aTeam.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </template>
          <!-- 普通用户 -->
          <template v-if="form.newRole == '2'"></template>
        </template>

        <!-- 团队成员 -->
        <template v-if="form.role == '1'">
          <template v-if="form.newRole == '0'"></template>
          <template v-if="form.newRole == '2'"></template>
        </template>

        <!-- 普通用户 -->
        <template v-if="form.role == '2'">
          <template v-if="form.newRole == '0'"></template>
          <template v-if="form.newRole == '1'">
            <el-form-item label="团队" prop="teamId">
              <el-select v-model="form.teamId" placeholder="请选择团队">
                <el-option
                  v-for="aTeam in aTeamList"
                  :key="aTeam.id"
                  :label="aTeam.teName"
                  :value="aTeam.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </template>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormIdentity">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAUser, getAUser, delAUser, addAUser, updateAUser, updateAUserIdentity } from "@/api/system/aUser";
import {changeUserStatus} from "@/api/system/user";
import { listAllATeam } from "@/api/system/aTeam";

export default {
  name: "AUser",
  dicts: ['agent_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序用户表格数据
      aUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openIdentity: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "create_time",
        isAsc: "desc",
        isShow: null,
        numSoft: null,
        miniOpenId: null,
        openId: null,
        phone: null,
        nickName: null,
        headImg: null,
        realName: null,
        role: null,
        downloadNum: null
      },
      // 表单参数
      form: {},
      aTeamList: [],
      // 表单校验
      rules: {
        openId: [
          { required: true, message: "appOpenId不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        newRole: [
          { required: true, message: "角色不能为空", trigger: "blur" }
        ],
        teamId: [
          { required: true, message: "团队不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getATeamList();
  },
  methods: {
    getATeamList(){
      listAllATeam().then(response => {
        this.aTeamList = response.data;
      });
    },
    handleRoleChange(e){
      console.log(e);
      this.form.newRole = e;
      this.$forceUpdate();
    },
    /** 查询小程序用户列表 */
    getList() {
      this.loading = true;
      listAUser(this.queryParams).then(response => {
        this.aUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openIdentity = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        isShow: null,
        numSoft: null,
        miniOpenId: null,
        openId: null,
        phone: null,
        nickName: null,
        headImg: null,
        realName: null,
        role: null,
        newRole: null,
        downloadNum: null,
        teamId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小程序用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小程序用户";
      });
    },
    /** 调整用户身份按钮操作 */
    handleIdentity(row) {
      this.reset();
      const id = row.id || this.ids
      getAUser(id).then(response => {
        this.form = response.data;
        this.form.newRole = null;
        this.openIdentity = true;
        this.title = "调整用户身份";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              // this.open = false;
              // this.getList();
              this.handleUpdate(1)
            });
          } else {
            addAUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              // this.open = false;
              // this.getList();
            });
          }
        }
      });
    },
    /** 提交调整身份按钮 */
    submitFormIdentity() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAUserIdentity(this.form).then(response => {
              this.$modal.msgSuccess("调整成功");
              this.openIdentity = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除小程序用户编号为"' + ids + '"的数据项？').then(function() {
        return delAUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/aUser/export', {
        ...this.queryParams
      }, `aUser_${new Date().getTime()}.xlsx`)
    },
    // 用户状态修改
    handleStatusChange(row) {
      let form = {
        id: row.id,
        isExamine: row.isExamine
      }
      let text = row.isExamine === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.nickName + '"用户吗？').then(function() {
        return updateAUser(form);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.isExamine = row.isExamine === "0" ? "1" : "0";
      });
    },
  }
};
</script>
